// pages/datamove/datamove.js
import {
  mobileLogin,
  getCode
} from '../../utils/networkRequest.js'
import {
  errorMsg
} from '../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    downtime: 60,
    verCode: "",
    mobile: "",
    token:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      token: options.token
    })

  },
  //mobile, verifyCode, action
  dataMove: function(e) {
    let {token} = this.data;
    let {
      mobile,
      verifyCode, 
    } = e.detail.value;
    mobileLogin(mobile, verifyCode, 'phoneLogin', token, (r) => {
      if (r.code == 0) {
        wx.setStorage({ //将SessionInfo 放入本地缓存
          key: 'token',
          data: r.data,
        })
        wx.reLaunch({
          url: '../index/index',
        })
        errorMsg(r.message)
      } else {
        errorMsg(r.message)
      }
    })
  },
  getMobile: function(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  getVCode: function(e) {
    this.setData({
      verCode: e.detail.value
    })
  },
  //获取验证码
  getCode: function() {
    let self = this;
    let {
      mobile
    } = this.data;
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(mobile)) {
      errorMsg('手机号不正确');
    } else {
      if (this.data.downtime == 60) {
        getCode(mobile, "phoneLogin", (r) => {

          if (r.code == 0) {
            this.setData({
              downtime: --self.data.downtime
            })
            let siv = setInterval(() => {
              if (self.data.downtime > 1) {
                self.setData({
                  downtime: --self.data.downtime
                })
              } else {
                clearInterval(siv);
                self.setData({
                  downtime: 60
                })
              }

            }, 1000)
          } else {
            errorMsg(r.message);
          }
        })
      }
    }

  }
})